import Vue from 'vue'
import { login } from '@/api/manage/base'
import { ACCESS_TOKEN } from '@/store/mutation-types'

import {
  SysGetEnterpriseIdInfo
} from '@/api/manage/enterprise'
import {
  sysGetParkIdInfo
} from '@/api/manage/park'
import {
  allGetSttleEnterpriseIsOpenAccount
} from '@/api/manage/sttle'
import {
  sysEditOperatePwdUser 
} from '@/api/manage/user'
const user = {
  state: {
    token: '',
    openState:false,
    userDetail: {},
    enterAccount:{},
    roles: []
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_USER_DETAIL: (state, row) => {
      state.userDetail = row
    },
    SET_ENTERPRISE_ACCOUNT: (state, row) => {
      state.enterAccount = row
    },
    SET_STATE: (state, status) => {
      state.openState = status
    },
  },

  actions: {
    Login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const result = response.data;
          Vue.ls.set(ACCESS_TOKEN, result.token, 7 * 24 * 60 * 60 * 1000)
          commit('SET_TOKEN', result.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    Logout ({ commit, state }) {
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        Vue.ls.remove(ACCESS_TOKEN)
        resolve()
      })
    },
    allGetCanmpanyDetail({ commit }) {
      return new Promise((resolve, reject) => {
        const userType = this.state.permission.info.type;
        const relationId = this.state.permission.info.relationId;
        if (userType === 2) {
          SysGetEnterpriseIdInfo(relationId).then(res => {
            if (res.code === '1') {
              commit('SET_USER_DETAIL', res.data)
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        } else if (userType === 3) {
          sysGetParkIdInfo(relationId).then(res => {
            if (res.code === '1') {
              commit('SET_USER_DETAIL', res.data)
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        }
        resolve();
      })
    },
    allGetSttleEnterpriseIsOpenAccount({ commit }) {
      return new Promise((resolve, reject) => {
        const userType = this.state.permission.info.type;
        const relationId = this.state.permission.info.relationId;
        if (userType === 2) {
          allGetSttleEnterpriseIsOpenAccount(relationId).then(res => {
            if (res.code === '1') {
              commit('SET_ENTERPRISE_ACCOUNT', res.data)
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        }
        resolve();
      })
    },
    openStateSet({ commit },status) {
      return new Promise((resolve, reject) => {
        commit('SET_STATE', status)
        resolve();
      })
    },
    sysEditOperatePwdUser({ commit },params) {
      return new Promise((resolve, reject) => {
       
          sysEditOperatePwdUser(params).then(res => {
            if (res.code === '1') {
              commit('SET_STATE', false)
            }
            resolve(res);
          }).catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default user
